<!--
<template>
  &lt;!&ndash; 添加或修改销售信息管理对话框 &ndash;&gt;
  <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="form.goodsName" placeholder="请输入商品名称" disabled/>
      </el-form-item>
      <el-form-item label="商品品牌" prop="goodsBrand">
        <el-input v-model="form.goodsBrand" placeholder="请输入商品品牌" disabled/>
      </el-form-item>
      <el-form-item label="销售单价" prop="goodsUnitPrice">
        <el-input-number :precision="2" v-model="form.goodsUnitPrice" :min="0" placeholder="请输入销售单价" />
        <span style="color: red;margin-left: 20px;">采购单价:{{orderInfo.costPrice}}</span>
      </el-form-item>
      <el-form-item label="销售数量" prop="goodsCount">
        <el-input-number v-model="form.goodsCount" placeholder="请输入销售数量" :min="0" :step="1" style="width: 200px;" />
        <span style="color: red;margin-left: 20px;">库存数量:{{orderInfo.remainderCount}}</span>
      </el-form-item>
      <el-form-item label="总价" prop="goodsTotalPrice">
        <el-input-number :precision="2" v-model="form.goodsTotalPrice" placeholder="请输入总价" disabled/>
        <span style="color: red;margin-left: 20px;">含税总价:{{orderInfo.costTax}}</span>
      </el-form-item>
      <el-form-item label="销售日期" prop="saleDate">
        <el-date-picker clearable
                        v-model="form.saleDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择销售日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="form.supplierId" placeholder="请选择供应商" filterable @change="selectSupplier" disabled
                   style="width: 300px">
          <el-option v-for="dict in dict.type.goods_supplier" ref="myselectSsupplier"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select v-model="form.customerId" placeholder="请选择客户" filterable @change="selectCustomer" clearable
                   style="width: 300px">
          <el-option v-for="dict in dict.type.goods_customer" ref="myselectCustomer"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      &lt;!&ndash;<el-form-item label="是否回款" prop="fundCallback">
        <el-select v-model="form.fundCallback" placeholder="是否取得发票" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.yes_no"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否打印签收单" prop="receipt">
        <el-select v-model="form.receipt" placeholder="是否打印签收单" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.yes_no"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>&ndash;&gt;
      &lt;!&ndash;<el-form-item label="销售人" prop="saleId">
        <el-select v-model="form.saleId" placeholder="请选择采购人" @change="selectUser" clearable style="width: 300px">
          <el-option v-for="dict in dict.type.sys_user" ref="myselectUser"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>&ndash;&gt;
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {addAction, getById, updateAction} from '@/api/manage'

    export default {
        name: "GoodsSale",
        dicts: [ 'goods_supplier', 'goods_customer'],
        data() {
            return {
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    goodsTotalPrice: [
                        {required: true, message: "销售总价不能为空", trigger: "blur"}
                    ],
                    customerId: [
                        {required: true, message: "客户信息不能为空", trigger: "blur"}
                    ],
                },
                url: {
                    getById: '/goodsSale/goodsSale/',
                    add: '/goodsSale/goodsSale/add',
                    addForSale: '/goodsSale/goodsSale/addForSale',
                    edit: '/goodsSale/goodsSale/edit',
                },
                orderInfo:{},
                min0:0,
            };
        },
        created() {

        },
        methods: {
            updateTotlePrice() {
                let that = this;
                if (that.form.goodsCount && that.form.goodsUnitPrice) {
                    let totlePrice = this.functionNumberMul(that.form.goodsCount, that.form.goodsUnitPrice);
                    that.$set(that.form, "goodsTotalPrice", totlePrice);
                }
            },
            functionNumberMul(arg1, arg2) {
                var m = 0;
                var s1 = arg1.toString();
                var s2 = arg2.toString();
                try {
                    m += s1.split(".")[1].length;
                } catch (e) {
                }
                try {
                    m += s2.split(".")[1].length;
                } catch (e) {
                }
                return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
            },
            selectCustomer(value) {
                let that = this;
                that.$nextTick(() => {
                    let arr = that.$refs.myselectCustomer;
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i].currentValue == value) {
                            that.form.customerName = arr[i].label;
                        }
                    }
                })
            },
            selectSupplier(value) {
                let that = this;
                that.$nextTick(() => {
                    let arr = that.$refs.myselectSsupplier;
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i].currentValue == value) {
                            that.form.supplierName = arr[i].label;
                        }
                    }
                })
            },
            selectUser(value) {
                let that = this;
                that.$nextTick(() => {
                    let arr = that.$refs.myselectUser;
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i].currentValue == value) {
                            that.form.saleName = arr[i].label;
                        }
                    }
                })
            },
            sale(row) {
                let that = this;
                that.open = true;
                that.title = "销售";
                that.orderInfo = row;
                that.form.goodsId = row.id;
                that.form.goodsName = row.name;
                that.form.goodsBrand = row.brand;
                that.form.goodsUnitPrice = row.costPrice;
                that.form.goodsCount = row.remainderCount;
                that.form.costTax = row.costTax;
                that.form.supplierId = row.supplierId;
                that.form.supplierName = row.supplierName;
                that.form.saleId = that.$store.getters.userId;
                that.form.saleName = that.$store.getters.nickName;
                that.form.saleDate = new Date();
                that.form.goodsTotalPrice = that.functionNumberMul(row.costPrice, row.remainderCount);
                /*console.log(that.$store.state.user.userId);
                console.log(that.$store.state.user.nickName);*/

            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    goodsId: null,
                    goodsName: null,
                    goodsBrand: null,
                    goodsUnitPrice: null,
                    goodsCount: null,
                    goodsTotalPrice: null,
                    saleDate: new Date(),
                    supplierId: null,
                    supplierName: null,
                    customerId: null,
                    customerName: null,
                    invoice: "0",
                    invoiceId: null,
                    invoiceNo: null,
                    fundCallback: "0",
                    receipt: null,
                    saleId: null,
                    saleName: null,
                    delFlag: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null,
                    remark: null
                };
                this.resetForm("form");
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加销售信息管理";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids;
                getById(this.url.getById, id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改销售信息管理";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if(this.form.goodsCount > this.orderInfo.remainderCount ){
                        this.$modal.msgError("销售数量不能大于库存数量!");
                        return;
                    }
                    if (valid) {
                        if (this.form.id != null) {
                            updateAction(this.url.edit, this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        } else {
                            addAction(this.url.addForSale, this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        }
                    }
                });
            },
        }
    };
</script>
-->
